﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using visinia.SmartEngine.Providers;

namespace visinia.SmartEngine
{
    public class SqlDataProvider : SmartEngineDataProvider
    {
        #region INIT
        private static string _sqlConnectionString = null;

        public SqlDataProvider()
        {
            _sqlConnectionString = ProviderController.GetDataProviderConnectionString();
        }
        public string ConnectionString
        {
            get { return _sqlConnectionString; }
        }
        #endregion

        #region MODULES
        public override IDataReader AddModule(int ModuleInstalledId, string Title, string TitleImageUrl, string TitleImageNavigationUrl, string CssClass, int Width, int Height, int PageId, string Pane, int PaneOrder, bool Visible)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "ModulesAddModule", ModuleInstalledId, Title, TitleImageUrl, TitleImageNavigationUrl, CssClass, Width, Height, PageId, Pane, PaneOrder, Visible);
        }

        public override void DeleteModule(int ModuleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "ModulesDeleteModule", ModuleId);
        }

        public override void UpdateModule(int ModuleId, int ModuleInstalledId, string Title, string TitleImageUrl, string TitleImageNavigationUrl, string CssClass, int Width, int Height, int PageId, string Pane, int PaneOrder, bool Visible)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "ModulesUpdateModule", ModuleId, ModuleInstalledId, Title, TitleImageUrl, TitleImageNavigationUrl, CssClass, Width, Height, PageId, Pane, PaneOrder, Visible);
        }

        public override IDataReader GetModule(int ModuleId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "ModulesGetModule", ModuleId);
        }

        public override IDataReader GetModules(int PageId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "ModulesGetModules", PageId);
        }        
        #endregion

        #region ModulePrivateSettings
        public override IDataReader GetModulePrivateSettings(int ModuleId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "ModulesGetModulePrivateSettings", ModuleId);
        }

        public override void AddModulePrivateSetting(int ModuleId, string Key, string Value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "ModulesAddModulePrivateSetting", ModuleId, Key, Value);
        }

        public override void UpdateModulePrivateSetting(int ModuleId, string Key, string Value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "ModulesUpdateModulePrivateSetting", ModuleId, Key, Value);
        }

        public override void DeleteModulePrivateSettings(int ModuleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "ModulesDeleteModulePrivateSettings", ModuleId);
        }
        public override void DeleteModulePrivateSetting(int ModuleId, string Key)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "ModulesDeleteModulePrivateSetting", ModuleId, Key);
        }
        #endregion

        #region PAGES
        public override IDataReader AddPage(string Name, string Title, int PluginId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "PagesAddPage", Name, Title, PluginId);
        }

        public override void DeletePage(int PageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "PagesDeletePage", PageId);
        }

        public override void UpdatePage(int PageId, string Name, string Title, int PluginId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "PagesUpdatePage", PageId, Name, Title, PluginId);
        }
        public override IDataReader GetPages()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "PagesGetPages");
        }
        #endregion        

        #region REPOSITORY
        public override IDataReader GetInstalledModules()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "RepositoryGetInstalledModules");
        }
        public override IDataReader RegisterModule(string Name, string Package, string ModuleUrl, string ModuleSettingsUrl, string Icon, string Image, string Help)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "RepositoryRegisterModule", Name, Package, ModuleUrl, ModuleSettingsUrl, Icon, Image, Help);
        }
        public override void UnRegisterModule(int ModuleInstalledId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "RepositoryUnRegisterModule", ModuleInstalledId);
        }
        #endregion

        #region SITE SETTINGS
        public override IDataReader GetSiteSettings()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "SiteGetSiteSettings");
        }

        public override void AddSiteSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SiteAddSiteSetting", key, value);
        }

        public override void UpdateSiteSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SiteUpdateSiteSetting", key, value);
        }

        public override void DeleteSiteSetting(string key)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SiteDeleteSiteSetting", key);
        }
        #endregion

        #region INSTALLER
        public override bool ExecuteScript(string script)
        {
            string[] sep = { "GO" };
            string[] array = script.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            SqlConnection connection = new SqlConnection(ConnectionString);
            connection.Open();
            foreach (string sql in array)
            {
                try
                {
                    SqlCommand command = new SqlCommand(sql, connection);
                    command.CommandTimeout = 0;
                    command.ExecuteNonQuery();
                }
                catch //ON ERROR just skip error and continue
                { continue; }
            }
            connection.Close();
            return true;
        }         
        #endregion

        #region PLUGINS
        public override IDataReader GetInstalledPlugins()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "PluginsGetInstalledPlugins");
        }
        public override IDataReader AddPlugin(string Name, string Description, string UrlSeparator, string UrlRewriteController, string SitemapController, string Version)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "PluginsAddPlugin", Name, Description, UrlSeparator, UrlRewriteController, SitemapController, Version);
        }        
        public override IDataReader UpdatePlugin(int PluginId, string Name, string Description, string UrlSeparator, string UrlRewriteController, string SitemapController, string Version)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "PluginsUpdatePlugin", PluginId, Name, Description, UrlSeparator, UrlRewriteController, SitemapController, Version);
        }        
        public override void DeletePlugin(int PluginId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "PluginsDeletePlugin", PluginId);
        }        
        #endregion     
        
        #region LOG
        public override void AddLog(string user, DateTime dateTime, string message)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "LogAddLog", user, dateTime, message);
        }
        public override IDataReader GetLog()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "LogGetLog");
        }
        #endregion                
    }
}
